﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;


namespace WebSys.UI.Control.ZhengShou
{
    public class RC_FWSelectControl : MPBasicTemplate
    {
        TextBox txtTitle;
        Button btnSeach, btnInsert;
        CustomGridView CustomGridList1;
        MPCalendar txtFsDate, txtJsDate;
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] != null)
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!Page.IsPostBack)
            {
                Bind();   
            }
            if (Page.Request["Id"]!=null)
            {
                DelFW();
            }
        }

        

        protected void InitControl()
        {
            txtTitle = (TextBox)FindControl("txtTitle");
            txtFsDate = (MPCalendar)FindControl("txtFsDate");
            txtJsDate = (MPCalendar)FindControl("txtJsDate");
            btnSeach = (Button)FindControl("btnSeach");
            btnSeach.Click += new EventHandler(btnSeach_Click);
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            btnInsert = (Button)FindControl("btnInsert");
            btnInsert.Click += new EventHandler(btnInsert_Click);
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert1", "<script>location.href('RC_FWEdit.aspx');</script>");
        }

        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnSeach_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void Bind()
        {

            string title = txtTitle.Text;
            string date =txtFsDate.Text;
            string jsdate = txtJsDate.Text;
            string sqlwhere = string.Empty;
            if (!string.IsNullOrEmpty(title))
            {
                sqlwhere += " and 主题 like '%" + title + "%'";
            }
            if (!string.IsNullOrEmpty(date))
            {
                sqlwhere += " and 发送时间 >= to_date('" + date + "','yyyy-mm-dd')";

            }
            if (!string.IsNullOrEmpty(jsdate))
            {
                sqlwhere += " and 发送时间 < to_date('" + jsdate + "','yyyy-mm-dd') ";
            }
            string sql = @"Select Id,主题,内容,接收人员,发送人员,发送时间,(Case When 是否已读 = '0' Then '否' Else '是' End  ) 是否已读 ,接收人员id From Zs_Gw Where Id In (Select itemid From ZS_FWFILE)
And 接收人员id Like '%" + userid + "%'" + sqlwhere + " Order By 是否已读";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.DataBind();
        }


        // 删除公文
        protected void DelFW()
        {
            string sql = "delete zs_gw where id = '" + Page.Request["Id"] + "'";
            _EluZSDataProvider.RunSqlNon(sql);
            Bind();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！')</script>");
        }
    }
}
